package Utils;

//import javax.mail.Address;
import Configs.MailConfig;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import static Utils.MyLogger.logInfo;


/**
 * @author 锄禾
 * @projectName MkPack
 * @description: 邮件类==>发送邮件
 */
public class SendMail {

    public static Properties proPerties;
    public static String from;
    public static String username;
    public static String password;
    public static String host;
    public static Session session;
    public static String Subject = "稳定性测试报告";
    public static String Content = getCurrentTime() + "\n" + String.format("详见%s附件",Subject);

    /**
    　* @description: 获取当前时间
    　* @param
    　* @return
    　*/
    private static String getCurrentTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String CurrentTime = df.format(new Date());
        logInfo("当前时间为:" + CurrentTime);
        return CurrentTime;
    }


    public void sendMail(String maillist, String filePath, String crashResult){
        File file = new File(filePath);
        setProperties();
        send(file, Address(maillist),crashResult);
    }

    /**
    　* @description: 设置邮件服务器参数，服务端口等参数
    　* @param
    　* @return
    　*/
    public void setProperties(){
        from = MailConfig.mail_user;
        username = MailConfig.mail_user;
        password = MailConfig.mail_pwd;
        host = MailConfig.mail_host;
        proPerties = new Properties();
        proPerties.put("proHost",MailConfig.mail_postfix);
        proPerties.put("mail.smtp.host",host);
        proPerties.put("main.smtp.auth","true");

        // 输出发送邮件的debug信息
        proPerties.put("mail.debug","true");
        proPerties.put("mail.transport.protocol","smtp");

        // 设置Session对象，同时配置验证
        session = Session.getInstance(proPerties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //return super.getPasswordAuthentication();
                return new PasswordAuthentication(username, password);
            }
        });
    }

    public void send(File attachment, InternetAddress[] addresses, String crashResult){

    }

    /**
    　* @description: 收件人地址以为,拼接
    　* @param
    　* @return
    　*/
    public static InternetAddress[] Address(String maillist){
        InternetAddress[] internetAddresses = null;
        try {
            List li = new ArrayList();
            String[] emper = maillist.split(",");
            for (int i = 0; i < emper.length; i++) {
                li.add(new InternetAddress(emper[i]));
            }
            internetAddresses = (InternetAddress[])li.toArray(new InternetAddress[li.size()]);
        } catch (AddressException e) {
            e.printStackTrace();
        }
        return internetAddresses;
    }
}
